<?php
class Delivery_Model_DbTable_MailsQueue
	extends Delivery_Model_DbTable_Abstract

{
	protected $_name = 'mails_queue';
	protected $_primary = 'idMailQueue';
	
	public function insert( $mail )
	{
		return parent::insert(		
			$this->_fecha( $this->_getData( $mail ), true, false)
		);	
	}
	
	public function update( $mail )
	{
		return parent::update(
			$this->_fecha( $this->_getData( $mail ), false, false),
			array('idMailQueue = ?' => $mail->idMailQueue)		
		);
	}
	
	public function delete ( $mail )
	{
		return parent::delete(array('idMailQueue = ?' => $mail->idMailQueue));
	}
	
	public function getAll( $limit )
	{
	    $select = $this->select()
					->where ('priority > 0')
					->order('priority DESC')
					->limit($limit);
			
		return $this->_db->fetchAll($select);	
	}
	
	protected function _getData( $mail )
	{
		return array (
			'idMailQueue'	=> $mail->idMailQueue,
   			'priority'		=> $mail->priority,	   			
   			'fromName'		=> $mail->fromName,
   			'fromAddress'	=> $mail->fromAddress,
   			'to'		    => $mail->to,
   			'subject'		=> $mail->subject,
   			'body'		    => $mail->body,
   			'trySent'		=> $mail->trySent,
   		);
	} 
	
	
}